<template>
    <view class="content">
        <u-cell-group>
            <u-swipe-action v-for="item in visitlist" :key="item.id">
                <u-swipe-action-item :options="options2" @click="changeOperation" :name="item.id">
                    <view class="swipe-action u-border-top u-border-bottom">
                        <view class="swipe-action__content">
                            <u-cell :title="item.content" :value="item.business.nickname" :label="$u.timeFormat(item.createtime, 'yyyy年mm月dd日 hh:MM')"
                                center></u-cell>
                        </view>
                    </view>
                </u-swipe-action-item>
            </u-swipe-action>
        </u-cell-group>
        <u-button @click="add" :customStyle="btnStyle" color="#19BC9C" type="success">添加回访记录</u-button>
        <u-modal :show="show" :content='content' :showCancelButton="true" @confirm="confirm"
            @cancel="show = false"></u-modal>
        <u-toast ref="uToast"></u-toast>
    </view>
</template>

<script>
export default {
    data() {
        return {
            btnStyle: {
                width: '100%',
                position: 'fixed',
                bottom: '0px'
            },
            visitlist: [],
            LoginUser: {},
            options2: [{
                text: '编辑',
                id: 'edit',
                style: {
                    backgroundColor: '#3c9cff'
                }
            }, {
                text: '删除',
                id: 'del',
                style: {
                    backgroundColor: '#f56c6c'
                }
            }],
            show: false,
            content: '确认是否删除当前回访记录?',
            id: 0
        }
    },
    onLoad()
    {
        this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {}

        this.getVisitData()
    },
    methods: {
        async getVisitData()
        {
            let result = await this.$u.api.business.index({adminid:this.LoginUser.id})

            if(result.code === 1)
            {
                this.visitlist = result.data
            }
        },
        add()
        {
            // uView封装路由或者uniApp里的路由
            this.$u.route({
                url:'/packageA/visit/add'
            })
        },
        changeOperation(value)
        {
            if(value.index === 0)
            {
                this.$u.route({
                    url:'/packageA/visit/edit',
                    params:{
                        visitid:value.name
                    }
                })

                return false
            }else if(value.index === 1)
            {
                let auth = this.$u.auth.check()

                if(auth === true)
                {
                    this.id = value.name

                    this.show = true
                }
            }
        },
        async confirm()
        {
            this.show = false

            let result = await this.$u.api.business.del({visitid:this.id})

            if(result.code === 1)
            {
                this.$refs.uToast.show({
                    type:'success',
                    message:result.msg,
                    complete:() => {
                        this.getVisitData()
                    }
                })

                return false
            }else{
                this.$refs.uToast.show({
                    type:'error',
                    message:result.msg
                })
                return false
            }
        }
    },
}
</script>

<style>
.content {
    height: 100vh;
}
</style>